unit ufrmLog;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ufrmAppDlgBase, ExtCtrls, StdCtrls, ComCtrls, Log, Buttons, FileCtrl;

type
  TfrmLog = class(TfrmAppDlgBase)
    pnlClient: TPanel;
    pnlLeft: TPanel;
    Splitter1: TSplitter;
    Splitter2: TSplitter;
    pnlClientClient: TPanel;
    memDesc: TMemo;
    lvLog: TListView;
    Splitter3: TSplitter;
    btnCurLog: TBitBtn;
    DirectoryListBox: TDirectoryListBox;
    FileListBox: TFileListBox;
    procedure btnCurLogClick(Sender: TObject);
    procedure lvLogChange(Sender: TObject; Item: TListItem;
      Change: TItemChange);
    procedure FileListBoxDblClick(Sender: TObject);
  private
    FLogManager: TLogManager;
    procedure AssignLogsToTreeView;
    { Private declarations }
  protected
    procedure AssignLogToListView(ALogManager : TLogManager; ListView : TListView);
  public
    { Public declarations }
    class function ViewLog(ALogManager : TLogManager; ALogPath, ALogFileExt : string) : boolean;

    property LogManager : TLogManager read FLogManager write FLogManager;
  end;


implementation

uses uAppUtils;

{$R *.dfm}

{ TfrmLog }

procedure TfrmLog.AssignLogsToTreeView;
var
  I: Integer;
begin
end;

class function TfrmLog.ViewLog(ALogManager: TLogManager; ALogPath, ALogFileExt : string) : boolean;
var
  frmLog: TfrmLog;
begin
  frmLog := TfrmLog.Create(Application);
  try
    with frmLog do
    begin
      LogManager := ALogManager;

      DirectoryListBox.Directory := ALogPath;
      FileListBox.Mask := ALogFileExt;

      frmLog.btnCurLog.Click;

      Result := frmLog.ShowModal = mrOK;
    end;    // with
  finally // wrap up
    frmLog.Free;
  end;    // try/finally
end;

procedure TfrmLog.AssignLogToListView(ALogManager: TLogManager;
  ListView: TListView);
var
  I: Integer;
  ListItem : TListItem;
begin
  ListView.Items.BeginUpdate;
  try
    ListView.OnChange := nil;

    ListView.Items.Clear;

    for I := 0 to ALogManager.Count - 1 do    // Iterate
    begin
      ListItem := ListView.Items.Add;
      ListItem.Caption := ALogManager[i].UserName;
      ListItem.SubItems.Add(DateTimeToStr(ALogManager[i].DateTime));
      ListItem.SubItems.Add(ALogManager[i].GetDescOfLogType);
      ListItem.SubItems.Add(ALogManager[i].Desc);
    end;    // for
  finally // wrap up
    ListView.Items.EndUpdate;
    ListView.OnChange := self.lvLogChange;
  end;    // try/finally
end;

procedure TfrmLog.btnCurLogClick(Sender: TObject);
begin
  AssignLogToListView(LogManager, lvLog);
end;

procedure TfrmLog.lvLogChange(Sender: TObject; Item: TListItem;
  Change: TItemChange);
begin
  memDesc.Text := Item.SubItems[2];
end;

procedure TfrmLog.FileListBoxDblClick(Sender: TObject);
var
  ALog : TLogManager;
begin
  ALog := TLogManager.Create;
  try
    ALog.LoadFromFile(FileListBox.FileName);
    AssignLogToListView(ALog, lvLog);
  finally // wrap up
    ALog.Free;
  end;    // try/finally
end;

end.
